﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Reflection;
using Microsoft.SqlServer.Server;

public partial class UserDefinedFunctions
{
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlString Version(SqlString versionPart)
    {
        if (versionPart.IsNull)
            versionPart = "";


        string result = String.Empty;

        if(versionPart.Value.ToUpper() == "MAJOR")
            result = String.Format("{0}", Assembly.GetExecutingAssembly().GetName().Version.Major.ToString());
        else if (versionPart.Value.ToUpper() == "MINOR")
            result = String.Format("{0}", Assembly.GetExecutingAssembly().GetName().Version.Minor.ToString());
        else if (versionPart.Value.ToUpper() == "BUILD")
            result = String.Format("{0}", Assembly.GetExecutingAssembly().GetName().Version.Build.ToString());
        else
            result = String.Format("{0}.{1}.{2}",
                Assembly.GetExecutingAssembly().GetName().Version.Major.ToString(),
                Assembly.GetExecutingAssembly().GetName().Version.Minor.ToString(),
                Assembly.GetExecutingAssembly().GetName().Version.Build.ToString()
                );
        

        return result;
    }
};

